<template>
  <el-dialog :title="title" :visible.sync="open" width="75%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="98px">
        <!--<el-form-item label="销售类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="销售类型" clearable style="width: 130px">
            <el-option v-for="dict in dict.type.sale_type"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>-->
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="请输入客户名称"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="商品品牌" prop="goodsBrand">
          <el-input v-model="queryParams.goodsBrand" placeholder="请输入商品品牌"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <!--<el-form-item label="是否开票" prop="invoice">
          <el-select v-model="queryParams.invoice" placeholder="是否开票" clearable style="width: 130px">
            <el-option v-for="dict in dict.type.yes_no"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>-->
        <!--<el-form-item label="是否回款" prop="fundCallback">
          <el-select v-model="queryParams.fundCallback" placeholder="是否回款" clearable style="width: 130px">
            <el-option v-for="dict in dict.type.fund_callback"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>-->
        <!--<el-form-item label="销售人姓名" prop="saleName">
          <el-input v-model="queryParams.saleName" placeholder="请输入销售人姓名"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="客户名称" align="center" prop="customerName" width="160">
          <template slot-scope="scope">
            {{scope.row.customerName}}
          </template>
        </el-table-column>
        <!--<el-table-column label="项目名称" align="center" prop="projectId">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.project_name" :value="scope.row.projectId"/>
          </template>
        </el-table-column>-->
        <el-table-column label="供应商名称" align="center" prop="supplierName"/>
        <el-table-column label="商品名称" align="center" prop="goodsName"/>
        <el-table-column label="商品品牌" align="center" prop="goodsBrand"/>
        <el-table-column label="单价" align="center" prop="goodsUnitPrice"/>
        <el-table-column label="销售数量" align="center" prop="goodsCount"/>
        <el-table-column label="总价" align="center" prop="goodsTotalPrice"/>
        <el-table-column label="销售人姓名" align="center" prop="saleName"/>
        <el-table-column label="销售日期" align="center" prop="saleDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否提交开票" align="center" prop="invoice">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.yes_no" :value="scope.row.invoice"/>
          </template>
        </el-table-column>
<!--        <el-table-column label="发票编号" align="center" prop="invoiceNo"/>-->
        <!--<el-table-column label="是否打印签收单" align="center" prop="receipt"/>-->
        <el-table-column label="是否回款" align="center" prop="fundCallback">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.fund_callback" :value="scope.row.fundCallback"/>
          </template>
        </el-table-column>
        <el-table-column label="销售类型" align="center" prop="type">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sale_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-popconfirm :title="`确认移除当前数据项？`" @confirm="handleDelete(scope.row)"
                           style="margin-left: 10px;">
              <el-button v-if="scope.row.type==1" slot="reference" size="mini" type="text" icon="el-icon-delete"
                         v-hasPermi="['develop:GoodsSale:remove']">移除
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>-->
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>

      <!--    <goodsSale-form ref="form" @ok="getList"></goodsSale-form>-->
      <!--    <goods-invoice-batch ref="goodsInvoiceBatch" @ok="getList"></goods-invoice-batch>-->
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="selectSaleSuccess">选 择</el-button>
      <el-button @click="cancelSelect">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>

    import {deleteAction, getAction, getById} from '@/api/manage'
    // import GoodsSaleForm from '@/views/develop/goodsSale/module/GoodsSaleForm'
    // import GoodsInvoiceBatch from '@/views/develop/goodsInvoice/module/GoodsInvoiceBatch'

    export default {
        name: "GoodsSaleListSelectForInvoice",
        dicts: ['fund_callback', 'sale_type', 'yes_no'],
        components: {
            // GoodsSaleForm,
            // GoodsInvoiceBatch
        },
        data() {
            return {
                // 弹出层标题
                title: "选择销售记录",
                // 是否显示弹出层
                open: false,
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 销售信息管理表格数据
                dataList: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 20,
                    goodsId: null,
                    goodsName: null,
                    goodsBrand: null,
                    goodsUnitPrice: null,
                    goodsCount: null,
                    goodsTotalPrice: null,
                    saleDate: null,
                    supplierId: null,
                    supplierName: null,
                    customerId: null,
                    customerName: null,
                    invoice: null,
                    invoiceId: null,
                    invoiceNo: null,
                    fundCallback: null,
                    receipt: null,
                    saleId: null,
                    saleName: null,
                    params: {
                        selectedIds: ""
                    },
                },
                url: {
                    listIds: '/goodsSale/goodsSale/listIds/',
                    list: '/goodsSale/goodsSale/list',
                    remove: '/goodsSale/goodsSale/',
                    export: '/goodsSale/goodsSale/export',
                    delete: '/goodsSale/goodsSale/deleteAndModidyOrderin/',
                },
            };
        },
        created() {
            // this.getList();
        },
        methods: {

            selectSaleSuccess() {
                let that = this;
                that.$emit('ok', that.selectRows);
                that.open = false;
            },
            cancelSelect() {
                this.open = false;
                this.selectRows = [];
            },
            //打开选择商品的列表,并将已存在商品id接收过来,作为过滤条件
            showSelectSaleDialog(selectedIds, customerId) {
                let that = this;
                that.open = true;
                that.queryParams.type = "1";//零散销售
                that.queryParams.invoice = "0";//未开票
                that.queryParams.fundCallback = "0";//未回款
                that.queryParams.customerId = customerId;
                that.queryParams.saleId = that.$store.getters.userId;
                that.queryParams.saleName = that.$store.getters.nickName;
                // that.queryParams.params.dataFlag = "1";
                that.queryParams.params.selectedIds = selectedIds;

                that.getList();
            },

            handleBatch(row) {
                const ids = row.id || this.ids;
                if (ids == null || ids == '' || ids == 'undefined') {
                    alert("请选择一条记录");
                } else {
                    getById(this.url.listIds, ids).then(response => {
                        if (response.rows.length > 0) {
                            this.$refs.goodsInvoiceBatch.show(ids);
                        } else {
                            alert("您选择多个客户的销售数据或销售数据中部分已经开具发票，请重新选择！");
                        }
                    });
                }
            },
            /** 查询销售信息管理列表 */
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.selectRows = selection;
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.$refs.form.handleAdd();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.$refs.form.handleUpdate(row);
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                deleteAction(this.url.delete, ids).then(response => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                this.download(this.url.export, {
                    ...this.queryParams
                }, `goodsSale_${new Date().getTime()}.xlsx`)
            },
        }
    };
</script>
